<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/28
 * Time: 14:21
 */

namespace app\admin\controller;


use app\admin\model\AlbumModel;
use app\admin\model\LabelsModel;
use think\Request;
use think\Config;

class Album extends Base
{
    protected $albumModel;
    protected $labelsModel;
    protected $labelsMaps;

    /*
     * 依赖注入Model
     * */
    public function __construct(AlbumModel $albumModel,LabelsModel $labelsModel){
        parent::__construct();
        $this->albumModel = $albumModel;
        $this->labelsModel = $labelsModel;
        $this->labelsMaps=[
            "status" => 1,
            "category" => 'album'
        ];
    }

    /*
    * 列表 album_index__label 与 album_index 权限一致
    * */
    public function album_index__label(){
        $labels=$this->labelsModel->getByMaps($this->labelsMaps);
        //每个分类相册的第一张图片做封面
        $thumb=[];
        foreach ($labels as $value){
            $code=$value['code'];
            $t=$this->albumModel->getAlbumThumb($code);
            if ($t['count']){
                $thumb[$code]['url']=$t['data']['url'];
                $thumb[$code]['count']=$t['count'];
            }
            else{
                $thumb[$code]['url']=Config::get("defultThumb");
                $thumb[$code]['count']=0;
            }
        }
        $this->assign("thumb",$thumb);
        $this->assign("labels",$labels);
        return $this->fetch('album/label');
    }

    /*
     * 列表
     * */
    public function album_index(){
        $label=input('get.code/s');
        $this->assign("code",$label);
        $this->assign("data",$this->albumModel->listAllByLabel($label));
        return $this->fetch('album/index');
    }

    /*
    * 新增
    * */
    public function album_add(){
        if(Request::instance()->isPost()){
            $files = request()->file('image');
            $datas=[];
            foreach ($files as $file){
                if($file){
                    $r_path=ROOT_PATH . 'public' . DS . 'uploads/album/'.input('post.label/s').'/';
                    $r_url='/uploads/album/'.input('post.label/s').'/';
                    $info = $file->validate(['ext'=>'gif,jpg,jpeg,bmp,png'])->rule('uniqid')->move($r_path);
                    if($info){
                        $data=input('post.');
                        $data['name']=$info->getFilename();
                        $data['path']=$r_path.$info->getSaveName();
                        $data['url']=$r_url.$data['name'];
                        unset($data['image']);
                        unset($data['__token__']);
                        $data['created_at']=time();
                        $data['updated_at']=time();
                        $validate=$this->albumModel->validate_model('add',$data);
                        if ($validate['code']){
                            array_push($datas,$data);
                        }else{
                            return $validate;
                        }
                    }
                }
            }
            if (count($datas))
                if($this->albumModel->add($datas))
                    return op_success();
            return op_fail();
        }
        $this->assign("label",input('get.code/s'));
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        return $this->fetch('album/add');
    }

    /*
    * 修改
    * */
    public function album_edit(){
        if(Request::instance()->isPost()){
            $data=input('post.');
            $file = request()->file('image');
            if($file){
                $r_path=ROOT_PATH . 'public' . DS . 'uploads/album/'.input('post.label/s').'/';
                $r_url='/uploads/album/'.input('post.label/s').'/';
                $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->rule('uniqid')->move($r_path);
                if($info){
                    $data['name']=$info->getFilename();
                    $data['path']=$r_path.$info->getSaveName();
                    $data['url']=$r_url.$data['name'];
                }
            }
            unset($data['image']);
            unset($data['id']);
            $validate=$this->albumModel->validate_model('edit',$data);
            if ($validate['code']){
                return $this->albumModel->edit($data);
            }
            return $validate;
        }
        $id=input('get.id/d');
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        $this->assign("data",$this->albumModel->getById($id));
        return $this->fetch('album/edit');
    }

    /*
    * 删除
    * */
    public function album_del(){
        return $this->albumModel->del();
    }


}